﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Windows.Web.Syndication;

namespace HDK.API.Syndication
{
    public static class Extensions
    {
        private static IList<Y> To<T,Y>(this IList<T> src, Func<T, Y> convert)
        {
            if (src == null)
                return new List<Y>();

            var r = from iter in src
                    where iter != null
                    select convert(iter);
            return r.ToList();
        }

        internal static IEnumerable<string> ExtractHTMLImages(this string Content)
        {
            if (string.IsNullOrEmpty(Content))
                yield break;

            Regex r = new Regex(@"img.*?(?:(?:\s+(src)=\""([^""]+)"")|(?:\s+[^\s]+))+.*/");

            var matches = r.Matches(Content);
            foreach (Match m in matches)

                if (m.Groups.Count > 1)
                {
                    string url = matches[0].Groups[2].Value;
                    if (string.IsNullOrEmpty(url))
                        continue;
                    //imgurls.Add(new Uri(url, UriKind.Absolute)); 
                    //Debug.WriteLine("Adding image " + url);
                    //Images.Add(new BitmapImage(new Uri(url, UriKind.Absolute)));
                    yield return url;// new Uri(url, UriKind.Absolute);
                }

        }
        public static SyndicationFeedBase ExportTo(this SyndicationFeed feed)
        {
            SyndicationFeedBase ret = new SyndicationFeedBase()
            {
                Title = feed.Title.TryGetNodeValue(),
                Description = feed.Subtitle.TryGetNodeValue(),
                LastUpdatedTime = feed.LastUpdatedTime
            };
            if (feed.Categories != null)
                ret.Categories = feed.Categories.To<SyndicationCategory, ISyndicationCategory>((cat) => new SyndicationCategoryBase()
                {
                    Label = cat.Label,
                    Term = cat.Term
                });
            if (feed.Authors != null)
                ret.Authors = feed.Authors.To<SyndicationPerson, ISyndicationPerson>((a) => new SyndicationPersonBase()
                {
                    Uri = a.Uri,
                    Name = a.Name,
                    Email = a.Email
                });
            if (feed.Contributors != null)
                ret.Contributors = feed.Contributors.To<SyndicationPerson, ISyndicationPerson>((a) => new SyndicationPersonBase()
                {
                    Uri = a.Uri,
                    Name = a.Name,
                    Email = a.Email
                });
            if (feed.Items != null)
                ret.Posts = feed.Items.To<SyndicationItem, ISyndicationPost>(i => new SyndicationPost()
                {
                    IsRead = false,
                    PublishedDate = i.PublishedDate,
                    Title = i.Title.TryGetNodeValue(),
                    Summary = i.Summary.TryGetNodeValue(),
                    Content = i.Content.TryGetNodeValue()
                });
            ret.ImageUrl = feed.ImageUri != null ? feed.ImageUri.ToString() : string.Empty;
 
            return ret;
        }
        public static string TryGetNodeValue(this ISyndicationText text)
        {
            return text == null ? string.Empty : text.NodeValue;
        }
    }
}
